package avicit.bdp.dgs.standard.rest;

import avicit.bdp.common.service.dto.CommonTypeDTO;
import avicit.bdp.core.constant.Constants;
import avicit.bdp.dgs.standard.dto.LookupTableDto;
import avicit.bdp.dgs.standard.service.LookupTableService;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author mayi 码表管理
 */
@RestController
@RequestMapping("/api/bdp/dgs/dataStandard/lookupTable/")
@Api(tags = "值域标准管理")
public class LookupTableRest {

  private final LookupTableService lookupTableService;

  @Autowired
  public LookupTableRest(LookupTableService lookupTableService) {
    this.lookupTableService = lookupTableService;
  }

  @ApiOperation(value = "分页查询")
  @ApiImplicitParams({
    @ApiImplicitParam(value = "分类ID", name = "typeId", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "状态", name = "status", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping
  public ResponseMsg<QueryRespBean<LookupTableDto>> getPageList(
      @RequestParam(required = false) String typeId,
      @RequestParam(required = false) String keyWords,
      @RequestParam(required = false) Integer status,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize)
      throws Exception {

    ResponseMsg<QueryRespBean<LookupTableDto>> responseMsg = new ResponseMsg<>();

    QueryRespBean<LookupTableDto> queryRespBean =
        lookupTableService.getPageList(typeId, keyWords, status, pageNo, pageSize);

    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @ApiOperation(value = "新增")
  @PostMapping
  public ResponseMsg<String> save(@RequestBody LookupTableDto lookupTableDto) {
    ResponseMsg<String> responseMsg = new ResponseMsg<>();
    lookupTableService.save(lookupTableDto);
    responseMsg.setResponseBody(lookupTableDto.getId());
    return responseMsg;
  }

  @ApiOperation(value = "修改")
  @PutMapping
  public ResponseMsg<Boolean> update(@RequestBody LookupTableDto lookupTableDto) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    lookupTableService.update(lookupTableDto);
    responseMsg.setResponseBody(true);
    return responseMsg;
  }

  @ApiOperation(value = "批量删除")
  @ApiParam(value = "逗号分隔的id串", name = "ids", required = true)
  @DeleteMapping
  public ResponseMsg<Boolean> batchDelete(@RequestBody String ids) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(lookupTableService.batchDelete(ids));
    return responseMsg;
  }

  @ApiOperation(value = "通过ID查询")
  @ApiParam(value = "id", name = "id", required = true)
  @GetMapping("/{id}")
  public ResponseMsg<LookupTableDto> queryById(@PathVariable String id) {
    ResponseMsg<LookupTableDto> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(lookupTableService.queryById(id));
    return responseMsg;
  }

  @ApiOperation(value = "查询树状结构数据")
  @ApiImplicitParams({
    @ApiImplicitParam(value = "查询关键字", name = "keyWords", dataType = "String"),
    @ApiImplicitParam(value = "数据空间id", name = "projectId", dataType = "String")
  })
  @GetMapping("/getTreeList")
  public ResponseMsg<List<CommonTypeDTO>> getTreeList(
      @RequestParam(required = false) String keyWords,
      @RequestParam(required = false) String projectId)
      throws Exception {

    ResponseMsg<List<CommonTypeDTO>> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(lookupTableService.getTreeList(projectId, keyWords));
    return responseMsg;
  }

  /** 下载模板 */
  @ApiOperation(value = "下载模板")
  @GetMapping("/downloadTemplate")
  public void downloadTemplate(HttpServletResponse response) throws Exception {
    lookupTableService.downloadTemplate(response);
  }

  @ApiOperation(value = "导出")
  @ApiImplicitParams({
    @ApiImplicitParam(value = "分类id", name = "typeId", dataType = "String"),
    @ApiImplicitParam(value = "ID列表，逗号分割", name = "ids", dataType = "String")
  })
  @GetMapping("/export")
  public void export(
      @RequestParam(required = false, name = "typeId") String typeId,
      @RequestParam(required = false, name = "ids") String ids,
      HttpServletResponse response)
      throws Exception {

    List<String> idList = null;
    if (StringUtils.isNotBlank(ids)) {
      idList = Arrays.asList(ids.split(Constants.COMMA));
    }
    lookupTableService.export(typeId, idList, response);
  }

  @PostMapping("/import")
  @ApiOperation(value = "导入excel")
  public ResponseMsg<String> importExcel(@RequestParam(value = "file") MultipartFile file)
      throws Exception {
    ResponseMsg<String> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(lookupTableService.importLookupTable(file));
    return responseMsg;
  }
}
